package OptimalAlgorithm.BitOperation;
//给定一个数组，包含从 1 到 N 所有的整数，但其中缺了两个数字。
//eg:
//输入：[2,3]
//输出：[1,4]
public class MissingTwo {
    public int[] missingTwo(int[] nums) {
        int tmp = 0;
        for (int i = 1; i <= nums.length + 2; i++) {
            tmp ^= i;
        }
        for (int num : nums) {
            tmp ^= num;
        }
        int x = tmp & -tmp;
        int num1 = 0;
        int num2 = 0;
        for (int i = 0; i < nums.length; i++) {
            if((x & nums[i]) == 0){
                num1 ^= nums[i];
            }else {
                num2 ^= nums[i];
            }
        }
        for (int i = 1; i <= nums.length + 2; i++) {
            if((x & i) == 0){
                num1 ^= i;
            }else {
                num2 ^= i;
            }
        }
        return new int[]{num1,num2};
    }
}
